<template>
  <div>
    <n-grid x-gap="10">
      <n-grid-item :span="10">
         <StoryBoardPicker :storyId="props.story.id"></StoryBoardPicker>
          <n-card title="文本总结" style="margin-top: 5px;" :header-style="{padding: '10px 10px 0px 10px'}" :content-style="{padding: '5px 5px 5px 5px'}">
            <template #header-extra>
                <n-button type="primary" size="small" @click="openSummaryPrompt" :loading="summaryLoading">总结文本
                </n-button>
            </template>
            <n-input
              type="textarea"
              style="height: 184px"
              v-if="story"
              v-model:value="props.story.summary"
            >
            </n-input>
          </n-card>
      </n-grid-item>
      <n-grid-item :span="14">

        <CharacterSetting :storyId="props.story.id" :storyContent="props.story.content"></CharacterSetting>
        <SceneSetting  :storyId="props.story.id"  :storyContent="props.story.content" style="margin-top: 5px;"></SceneSetting>
      </n-grid-item>
    </n-grid>


<!--    <n-drawer v-model:show="commonPromptChatShow" width="1200px">-->
<!--      <n-drawer-content title="大语言推理 - 总结文本" closable>-->
    <commonPromptChat :commonPromptSetting="commonPromptSetting" :closeAction="closeAction" v-model:show="commonPromptChatShow"></commonPromptChat>
<!--      </n-drawer-content>-->
<!--    </n-drawer>-->
  </div>

</template>

<script lang="ts" setup>
import {storeToRefs} from "pinia";
import {useSystemStore} from "@/store/modules/useSystemStore";
import {onMounted, ref} from "vue";
import StoryBoardPicker from "@/views/pages/create/novel/components/storyboardcomp/storyBoardPicker.vue";
import CharacterSetting from "@/views/pages/create/novel/components/characterSetting.vue";
import SceneSetting from "@/views/pages/create/novel/components/sceneSetting.vue";
import {storyController} from "@/api/story/story.api";
import CommonPromptChat from "@/components/gpt/common/commonPromptChat.vue";




const {
  clientHeight,
  layoutHeaderHeight,
  darkTheme,
  tabsHeight
} = storeToRefs(useSystemStore());
const summaryLoading = ref(false)
const commonPromptChatShow = ref(false)


const props = defineProps({
  story:{
    type: Object,
    required: true
  }
})


const commonPromptSetting :Chat.CommonPromptSetting = {
  bizType: "custom_summary",
  bizData: [],
  resultRequire: "",
  saveResultFunction: (results:any) => {
    summaryContent(results[0].result)
  },
  warningText: "推理文本过多会导致以下问题: \r 1) 大语言模型生成的时间过长，有可能会导致超时... \r 2) 超出模型的上下文限制，导致请求出错... \r 3) 建议文本字数控制在1000字以内...",
}

const openSummaryPrompt = () => {
  const bizData = ref([])
  const promptData = {
    'id': props.story.id,
    'index': 0 ,
    'content': props.story.content,
    'result': ''
  }
  bizData.value.push(promptData)
  commonPromptSetting.bizData =  bizData.value
  commonPromptChatShow.value = true
}

const summaryContent = (summary:string) => {
  summaryLoading.value = true
  const storySummary = {...props.story}
  storySummary.summary = summary
  storyController.saveSummary(storySummary).then(res => {
    summaryLoading.value = false
    if (res.code == 200) {
      props.story.summary = res.data
    }
    window.$message.success("总结文本成功...")
  }).finally(() => {
    summaryLoading.value = false
    commonPromptChatShow.value = false
  })
}

const closeAction = ()=>{
  commonPromptChatShow.value = false
}
</script>
